/**
 * 
 */
package com.sasis.webapp.task.ftp;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;

/**
 * @author Donitz
 * @since 2007-9-16
 */
public class GzipTools implements Compressor {
	private static Log log = LogFactory.getLog(GzipTools.class);
	/**
	 * 
	 */
	public GzipTools() {
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see com.sasis.webapp.task.Compressor#compress(java.lang.String, java.lang.String)
	 */
	public boolean compress(String fromFullPath, String toFullPath) {

		return false;
	}

	/* (non-Javadoc)
	 * @see com.sasis.webapp.task.Compressor#unCompress(java.lang.String, java.lang.String)
	 */
	public boolean unCompress(String fromFullPath, String toFullPath) {
		BufferedInputStream bufInput = null;
		BufferedOutputStream bufOutput = null;
		GZIPInputStream gzin = null;
		try {
			bufInput = new BufferedInputStream(new FileInputStream(fromFullPath));
			//Build gzip uncompress stream
		    gzin = new GZIPInputStream(bufInput);
			//Build gzip file output stream
		    bufOutput = new BufferedOutputStream(new FileOutputStream(toFullPath));
			byte[] buf = new byte[1024];
			int num;
			
			while ((num = gzin.read(buf, 0, buf.length)) != -1) {
				bufOutput.write(buf, 0, num);
			}
			gzin.close();			
			bufOutput.close();
			bufInput.close();
			
			return true;
		}  catch (IOException e) {
			log.error("ERROR UnCompress IO : "+fromFullPath+" >>> "+toFullPath,e);
		} finally{
			if(gzin!=null){
				try {
					gzin.close();
				} catch (IOException e1) {
					log.error("ERRO GZIPInputStream IO : "+fromFullPath+" >>> "+toFullPath,e1);
				}
			}
			if(bufOutput!=null){
				try {
					bufOutput.close();
				} catch (IOException e1) {
					log.error("ERRO BufferedOutputStream IO : "+fromFullPath+" >>> "+toFullPath,e1);
				}
			}
			if(bufInput!=null){
				try {
					bufInput.close();
				} catch (IOException e1) {
					log.error("ERROR BufferedInputStream IO : "+fromFullPath+" >>> "+toFullPath,e1);
				}
			}
		}
		return false;
	}
	
	
}
